<html>
<head><meta charset="utf-8"><title>What exactly is catch_unwind guaranteed to catch? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20exactly.20is.20catch_unwind.20guaranteed.20to.20catch.3F.html">What exactly is catch_unwind guaranteed to catch?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234156713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20exactly%20is%20catch_unwind%20guaranteed%20to%20catch%3F/near/234156713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John-John Tedro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20exactly.20is.20catch_unwind.20guaranteed.20to.20catch.3F.html#234156713">(Apr 12 2021 at 13:25)</a>:</h4>
<p>The <a href="https://doc.rust-lang.org/std/panic/fn.catch_unwind.html#notes">documentation of std::panic::catch_unwind</a> states that it's not guaranteed to catch all panics, with the following note:</p>
<blockquote>
<p>Note that this function may not catch all panics in Rust. A panic in Rust is not always implemented via unwinding, but can be implemented by aborting the process as well. This function only catches unwinding panics, not those that abort the process.<br>
Also note that unwinding into Rust code with a foreign exception (e.g. a an exception thrown from C++ code) is undefined behavior.</p>
</blockquote>
<p>What this leaves out from specifying is what exactly <code>catch_unwind</code> <em>is</em> guaranteed to catch? This can make a difference when choosing how to design something that deals with panics.</p>
<p>For example, is <code>catch_unwind</code> guaranteed to catch any panic that would otherwise cause a drop impl to be called when unwinding? I.e. are the following two code snippets equivalent from a "what panics are covered" perspective?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">panic</span>::<span class="n">catch_unwind</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="n">foo</span><span class="p">());</span><span class="w"></span>

<span class="k">if</span><span class="w"> </span><span class="n">result</span><span class="p">.</span><span class="n">is_err</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"A panic happened!"</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">std</span>::<span class="n">panic</span>::<span class="n">resume_unwind</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>And:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">LogOnPanic</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">LogOnPanic</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">drop</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"A panic happened!"</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">guard</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">LogOnPanic</span><span class="p">;</span><span class="w"></span>
<span class="n">foo</span><span class="p">();</span><span class="w"></span>
<span class="n">std</span>::<span class="n">mem</span>::<span class="n">forget</span><span class="p">(</span><span class="n">guard</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="234156987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20exactly%20is%20catch_unwind%20guaranteed%20to%20catch%3F/near/234156987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20exactly.20is.20catch_unwind.20guaranteed.20to.20catch.3F.html#234156987">(Apr 12 2021 at 13:26)</a>:</h4>
<p>catch_unwind will stop all unwinding triggered by a Rust panic. It will not stop the process from aborting if built with panic=abort, or catch things like C++ exceptions.</p>



<a name="234169243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20exactly%20is%20catch_unwind%20guaranteed%20to%20catch%3F/near/234169243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20exactly.20is.20catch_unwind.20guaranteed.20to.20catch.3F.html#234169243">(Apr 12 2021 at 14:32)</a>:</h4>
<p>or any other aborts (for example, panic-in-panic).</p>



<a name="234170119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20exactly%20is%20catch_unwind%20guaranteed%20to%20catch%3F/near/234170119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20exactly.20is.20catch_unwind.20guaranteed.20to.20catch.3F.html#234170119">(Apr 12 2021 at 14:37)</a>:</h4>
<p>(or <code>std::process::abort</code> <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>